package com.lhn.eventTest;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 * @author lhn
 * @date 2024-07-26 15:52
 */

public class BallMove extends JFrame{

    MyPanel myPanel = null;

    public static void main(String[] args) {
        BallMove ballMove = new BallMove();
    }

    public BallMove() {
        myPanel = new MyPanel();
        this.add(myPanel);
        this.setSize(400, 300);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        //窗口JFrame对象可以监听键盘事件，即可以监听到面板的键盘事件
        this.addKeyListener(myPanel);
        this.setVisible(true);

    }
}

class MyPanel extends JPanel implements KeyListener {

    Oval oval = new Oval(10, 10, 20, 20);

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillOval(oval.getX(), oval.getY(), oval.getWidth(), oval.getHeight());
    }

    //监听键盘字符输入
    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
//        System.out.println("key code is " + e.getKeyCode());

        //方向键
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            oval.setX(oval.getX() - 5);
        }
        if (e.getKeyCode() == KeyEvent.VK_UP) {
            oval.setY(oval.getY() - 5);
        }
        if (e.getKeyCode() == KeyEvent.VK_DOWN) {
            oval.setY(oval.getY() + 5);
        }
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            oval.setX(oval.getX() + 5);
        }

        //WASD
        switch (
                e.getKeyCode()
        ) {
            case 87:
//                System.out.println("上");
                oval.setY(oval.getY() - 5);
                break;
            case 65:
//                System.out.println("左");
                oval.setX(oval.getX() - 5);
                break;
            case 83:
//                System.out.println("下");
                oval.setY(oval.getY() + 5);
                break;
            case 68:
//                System.out.println("右");
                oval.setX(oval.getX() + 5);
                break;
        }

        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}

class Oval{
    private int x;
    private int y;
    private int width;
    private int height;
    public Oval() {
    }

    public Oval(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }
}